package controllers

import (
	"docker/models"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"gorm.io/gorm"
	"strconv"
	"time"
)

type DockerController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *DockerController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	es := c.GetString("es")
	time := c.GetString("time")
	num, _ := c.GetInt("num")
	status := c.GetString("status")
	docker := models.Docker{
		Name:   name,
		Sex:    sex,
		Tel:    tel,
		Es:     es,
		Time:   time,
		Num:    num,
		Status: status,
	}

	if name == "" || sex == "" || tel == "" || es == "" || time == "" || num == 0 || status == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "字段不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	err := models.DB.Create(&docker).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: docker,
		}
		c.ServeJSON()
	}
}

func (c *DockerController) List() {
	pageData := make(map[string]interface{})
	query := models.DB.Model(&models.Docker{})

	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)

	key := "docker-" + strconv.Itoa(page) + "-" + "name" + "-"
	val, err := models.Rdb.Get(key).Result()

	name := c.GetString("name")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}

	if err != nil {
		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		var dock []models.Docker
		err = query.Offset(offset).Limit(size).Find(&dock).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageData["dock"] = dock
			pageData["count"] = count
			jsonByte, _ := json.Marshal(pageData)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Second*2)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}

func (c *DockerController) Info() {
	id, _ := c.GetInt("id")

	RankKey := "docker-rank"
	key := "docker-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()

	var docker models.Docker
	if err != nil {
		err = models.DB.Where("ID=?", id).First(&docker).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "详情查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			models.Rdb.ZIncrBy(RankKey, 0, docker.Name)
			marshal, _ := json.Marshal(&docker)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情查询成功-mysql",
				Data: docker,
			}
			c.ServeJSON()
		}
	} else {
		models.Rdb.ZIncrBy(RankKey, 0, docker.Name)
		json.Unmarshal([]byte(val), &docker)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示失败-redis",
			Data: docker,
		}
		c.ServeJSON()
	}
}

func (c *DockerController) Rank() {
	key := "docker-rank"
	//nums := models.Rdb.ZCard(key).Val()
	ranks, err := models.Rdb.ZRevRangeWithScores(key, 0, 8-1).Result()
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "排序展示失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "排行展示成功",
			Data: ranks,
		}
		c.ServeJSON()
	}
}

func (c *DockerController) Up() {
	id, _ := c.GetInt("id")
	status := c.GetString("status")

	dock := models.Docker{
		Model:  gorm.Model{ID: uint(id)},
		Status: status,
	}
	err := models.DB.Where("ID=?", id).Updates(&dock).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: dock,
		}
		c.ServeJSON()
	}
}
